package com.tongji.housediy.view.component3D
{
	import alternativa.engine3d.core.Vertex;
	import alternativa.engine3d.materials.Material;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.engine3d.objects.Mesh;
	
	import flash.display.BitmapData;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class OutWall extends Mesh
	{
		public function OutWall(point1x:Number, point1y:Number, point2x:Number, point2y:Number, height:Number, material:TextureMaterial, borderChild:Vector.<Number>)
		{
			var borderChildLength:int = borderChild.length / 8;
			
			if (borderChildLength == 0)
			{
				var v1:Vertex = this.addVertex(point1x, point1y, 0, 0, 0);
				var v2:Vertex = this.addVertex(point2x, point2y, 0, 1, 0);
				var v3:Vertex = this.addVertex(point1x, point1y, height / 4, 0, 1 / 4);
				var v4:Vertex = this.addVertex(point2x, point2y, height / 4, 1, 1 / 4);
				var v5:Vertex = this.addVertex(point1x, point1y, height * 3 / 4, 0, 3 / 4);
				var v6:Vertex = this.addVertex(point2x, point2y, height * 3 / 4, 1, 3 / 4);
				var v7:Vertex = this.addVertex(point1x, point1y, height, 0, 1);
				var v8:Vertex = this.addVertex(point2x, point2y, height, 1, 1);
				this.addFace(Vector.<Vertex>([v3, v4, v2, v1]), material);
				this.addFace(Vector.<Vertex>([v5, v6, v4, v3]), material);
				this.addFace(Vector.<Vertex>([v7, v8, v6, v5]), material);
				this.calculateFacesNormals();
			}
			else
			{
				var wallLength:Number = Math.sqrt((point1x - point2x) * (point1x - point2x) + (point1y - point2y) * (point1y - point2y));
				for (var i:int = 0; i<borderChildLength; i++)
				{
					if (i == 0)
					{
						v1 = this.addVertex(point1x, point1y, 0, 0, 0);
						v2 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], 0, distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, 0);
						v3 = this.addVertex(point1x, point1y, height / 4, 0, 1 / 4);
						v4 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], borderChild[8 * i + 4], distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, borderChild[8 * i + 4] / height);
						v5 = this.addVertex(point1x, point1y, height * 3 / 4, 0,  3 / 4);
						v6 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], borderChild[8 * i + 5], distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, borderChild[8 * i + 5] / height);
						v7 = this.addVertex(point1x, point1y, height, 0, 1);
						v8 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], height, distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, 1);
						this.addFace(Vector.<Vertex>([v3, v4, v2, v1]), material);
						this.addFace(Vector.<Vertex>([v5, v6, v4, v3]), material);
						this.addFace(Vector.<Vertex>([v7, v8, v6, v5]), material);
					}
					else
					{
						v1 = this.addVertex(borderChild[8 * i - 6], borderChild[8 * i - 5], 0, distance(borderChild[8 * i - 6], borderChild[8 * i - 5],point1x, point1y) / wallLength, 0);
						v2 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], 0, distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, 0);
						v3 = this.addVertex(borderChild[8 * i - 6], borderChild[8 * i - 5], borderChild[8 * i - 4], distance(borderChild[8 * i - 6], borderChild[8 * i - 5],point1x, point1y) / wallLength, borderChild[8 * i - 4] / height);
						v4 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], borderChild[8 * i + 4], distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, borderChild[8 * i + 4] / height);
						v5 = this.addVertex(borderChild[8 * i - 6], borderChild[8 * i - 5], borderChild[8 * i - 3], distance(borderChild[8 * i - 6], borderChild[8 * i - 5],point1x, point1y) / wallLength, borderChild[8 * i - 3] / height);
						v6 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], borderChild[8 * i + 5], distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, borderChild[8 * i + 5] / height);
						v7 = this.addVertex(borderChild[8 * i - 6], borderChild[8 * i - 5], height, distance(borderChild[8 * i - 6], borderChild[8 * i - 5],point1x, point1y) / wallLength, 1);
						v8 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], height, distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, 1);
						this.addFace(Vector.<Vertex>([v3, v4, v2, v1]), material);
						this.addFace(Vector.<Vertex>([v5, v6, v4, v3]), material);
						this.addFace(Vector.<Vertex>([v7, v8, v6, v5]), material);
					}
					
					
					switch (borderChild[8 * i + 6])
					{
						case 0:
						{
							v1 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], borderChild[8 * i + 5], distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, borderChild[8 * i + 5] / height);
							v2 = this.addVertex(borderChild[8 * i + 2], borderChild[8 * i + 3], borderChild[8 * i + 5], distance(borderChild[8 * i + 2], borderChild[8 * i + 3],point1x, point1y) / wallLength, borderChild[8 * i + 5] / height);
							v3 = this.addVertex(borderChild[8 * i + 2], borderChild[8 * i + 3], height, distance(borderChild[8 * i + 2], borderChild[8 * i + 3],point1x, point1y) / wallLength, 1);
							v4 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], height, distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, 1);
							this.addFace(Vector.<Vertex>([v4, v3, v2, v1]), material);
							break;
						}
						case 1:
						{
							v1 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], 0, distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, 0);
							v2 = this.addVertex(borderChild[8 * i + 2], borderChild[8 * i + 3], 0, distance(borderChild[8 * i + 2], borderChild[8 * i + 3],point1x, point1y) / wallLength, 0);
							v3 = this.addVertex(borderChild[8 * i + 2], borderChild[8 * i + 3], borderChild[8 * i + 4], distance(borderChild[8 * i + 2], borderChild[8 * i + 3],point1x, point1y) / wallLength, borderChild[8 * i + 4] / height);
							v4 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], borderChild[8 * i + 4], distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, borderChild[8 * i + 4] / height);
							this.addFace(Vector.<Vertex>([v4, v3, v2, v1]), material);
							v1 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], borderChild[8 * i + 5], distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, borderChild[8 * i + 5] / height);
							v2 = this.addVertex(borderChild[8 * i + 2], borderChild[8 * i + 3], borderChild[8 * i + 5], distance(borderChild[8 * i + 2], borderChild[8 * i + 3],point1x, point1y) / wallLength, borderChild[8 * i + 5] / height);
							v3 = this.addVertex(borderChild[8 * i + 2], borderChild[8 * i + 3], height, distance(borderChild[8 * i + 2], borderChild[8 * i + 3],point1x, point1y) / wallLength, 1);
							v4 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], height, distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, 1);
							this.addFace(Vector.<Vertex>([v4, v3, v2, v1]), material);
							break;
						}
						case 2:
						{
							v1 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], borderChild[8 * i + 5], distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, borderChild[8 * i + 5] / height);
							v2 = this.addVertex(borderChild[8 * i + 2], borderChild[8 * i + 3], borderChild[8 * i + 5], distance(borderChild[8 * i + 2], borderChild[8 * i + 3],point1x, point1y) / wallLength, borderChild[8 * i + 5] / height);
							v3 = this.addVertex(borderChild[8 * i + 2], borderChild[8 * i + 3], height, distance(borderChild[8 * i + 2], borderChild[8 * i + 3],point1x, point1y) / wallLength, 1);
							v4 = this.addVertex(borderChild[8 * i + 0], borderChild[8 * i + 1], height, distance(borderChild[8 * i + 0], borderChild[8 * i + 1],point1x, point1y) / wallLength, 1);
							this.addFace(Vector.<Vertex>([v4, v3, v2, v1]), material);
							break;
						}
					}
					
				}
				v1 = this.addVertex(borderChild[8 * i - 6], borderChild[8 * i - 5], 0, distance(borderChild[8 * i - 6], borderChild[8 * i - 5],point1x, point1y) / wallLength, 0);
				v2 = this.addVertex(point2x, point2y, 0, 1, 0);
				v3 = this.addVertex(borderChild[8 * i - 6], borderChild[8 * i - 5], borderChild[8 * i - 4], distance(borderChild[8 * i - 6], borderChild[8 * i - 5],point1x, point1y) / wallLength, borderChild[8 * i - 4] / height);
				v4 = this.addVertex(point2x, point2y, height / 4, 1, 1 / 4);
				v5 = this.addVertex(borderChild[8 * i - 6], borderChild[8 * i - 5], borderChild[8 * i - 3], distance(borderChild[8 * i - 6], borderChild[8 * i - 5],point1x, point1y) / wallLength, borderChild[8 * i - 3] / height);
				v6 = this.addVertex(point2x, point2y, height * 3 / 4, 1, 3 / 4);
				v7 = this.addVertex(borderChild[8 * i - 6], borderChild[8 * i - 5], height, distance(borderChild[8 * i - 6], borderChild[8 * i - 5],point1x, point1y) / wallLength, 1);
				v8 = this.addVertex(point2x, point2y, height, 1, 1);
				this.addFace(Vector.<Vertex>([v3, v4, v2, v1]), material);
				this.addFace(Vector.<Vertex>([v5, v6, v4, v3]), material);
				this.addFace(Vector.<Vertex>([v7, v8, v6, v5]), material);
				this.calculateFacesNormals();
			}
		}
		
		
		public function distance(point1x:Number, point1y:Number, point2x:Number, point2y:Number):Number
		{
			return Math.sqrt((point1x - point2x) * (point1x - point2x) + (point1y - point2y) * (point1y - point2y));
		}
		
	}
}